package com.csc.finalproject.controller;
import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.csc.finalproject.service.UserService;
import com.csc.finalproject.service.UserServiceImpl;
/**
 * 
 * @author Nguyen Ngoc Truc Giang
 * @since 19/8
 *
 */
@Controller
public class LoginController {
	@Autowired
	public UserService uService;
	@RequestMapping(value = "/login", method = RequestMethod.GET)
	public String  loginPage()
	{
		return "login";
	}
	
	@RequestMapping(value = "/login", method = RequestMethod.POST)
	public String login(HttpServletRequest request,ModelMap model)
	{
		String username=request.getParameter("username");
		String password=request.getParameter("password");
		if((uService.checkLogin(username, password,request))==0)
		{
			model.addAttribute("error", "Invalid username or password");
		}
		else if((uService.checkLogin(username, password,request))==1)
		{
			return "forward:/getAccountInfoForAdmin.html";
		}
		else if((uService.checkLogin(username, password,request))==2)
		{
			return "forward:/getAccountInfoForSupport.html";
		}
		return "login";
	}
	/**@RequestMapping(value = "/", method = RequestMethod.GET)
	public void index(HttpServletResponse response) throws IOException {
		response.sendRedirect("login");
	}*/
}
